<!DOCTYPE html>
<html lang="en">
    <head>
        <?php include(__DIR__ . '/../common/header.phtml'); ?>
        <style type="text/css">
            #searchfloat {position:absolute;top:40px;right:20px;background:#F7F0A0;padding:10px;}
            #saved {position: relative;}
            #saved_sql {position:absolute;bottom:0;height:300px;background:#F7F0A0;width:100%;overflow:auto;display:none;}
            #saved_sql li {display:block;clear:both;width:100%;float:left;line-height:18px;padding:1px 0}
            #saved_sql li a{float:left;text-decoration: none;display:block;padding:0 5px;}
            #saved_sql li i{display:none;float:left;color:#06f;font-size: 14px;font-style: normal;margin-left:2px;line-height:18px;}
            #saved_sql li:hover{background:#fff;}
            #saved_sql li:hover i{display:block;cursor:pointer;}
            .dropdown-menu ul li{margin:3px 0;}
            #sqlquery {font-size:14px;}
        </style>
    </head>
    <body>
        <div class="wrapper">
            <?php include(__DIR__ . '/../common/menu.phtml'); ?>
            <div class="page-wrapper">
                <?php include(__DIR__ . '/../common/intro.phtml'); ?>
                <div class="page-content">

                    <div class="row">
                        <div class="col-md-4">
                            <h4>结果：</h4>
                        </div>
                        <div class="col-md-8 text-right">
                            <form action="<?= Tools::siteurl('database/query') ?>" method="post" name="infoform" target="resultframe">
                                <input type="hidden" name="do_action" id="topaction" />

                                <div class="btn-group">
                                    <button data-toggle="dropdown" class="btn btn-primary btn-embossed dropdown-toggle" type="button">基础查询 <span class="caret"></span></button>
                                    <div class="dropdown-menu pull-right">
                                        <div style="width:280px; background-color:#f1f1f2;padding:10px;border:1px solid #e3e3e3">
                                            <div class="pull-left">
                                                <select class="form-control" id="tablename" name="tablename[]" multiple="multiple" style="height:200px;width:160px;">
                                                    <?php foreach ($tables as $m => $n) { ?><option value="<?= $n['Name'] ?>" <?php if ($m % 2 == 0) { ?>style="background:#f7f7f7;"<?php } ?> title=""><?= $n['Name'] ?><!--(<?= $n['Rows'] ?>)--></option><?php } ?>
                                                    <option disabled>总计：<?= count($tables) ?>个表</option>
                                                    <option disabled>记录：<?= $tables_data_count ?>条</option>
                                                    <option disabled>占用：<?= Tools::formatBytes($tables_data_length) ?></option>
                                                    <option disabled>索引：<?= Tools::formatBytes($tables_index_length) ?></option>
                                                </select>
                                            </div>
                                            <div class="pull-right">
                                                <ul id="subaction">
                                                    <li><input type="submit" name="submit3" value="查看表结构" rel="viewinfo" class="btn btn-primary btn-embossed btn-sm btn-block"/></li>
                                                    <li><input type="submit" name="submit7" value="查看数据" rel="viewdata" class="btn btn-primary btn-embossed btn-sm btn-block"/></li>
                                                    <li><input type="submit" name="submit1" value="优化表" rel="optimize" class="btn btn-primary btn-embossed btn-sm btn-block" /></li>
                                                    <li><input type="submit" name="submit2" value="修复表" rel="repair" class="btn btn-primary btn-embossed btn-sm btn-block"/></li>
                                                    <li><input type="submit" name="submit5" value="优化全部表" rel="optimizeall" class="btn btn-primary btn-embossed btn-sm btn-block" /></li>
                                                    <li><input type="submit" name="submit6" value="修复全部表" rel="repairall" class="btn btn-primary btn-embossed btn-sm btn-block" /></li>
                                                </ul>
                                            </div>
                                            <div class="clear"></div>
                                        </div>
                                    </div>

                                </div>
                            </form>
                        </div>

                    </div>
                    <div class="well" style="padding:5px;">
                        <iframe name="resultframe" frameborder="0" id="resultframe" style="min-height:150px;" width="100%" height="100%"></iframe>
                    </div>
                    <form action="<?= Tools::siteurl('database/query') ?>" method="post" id="sqlexecute" name="form1" target="resultframe">
                        <input type="hidden" name="do_action" value="doquery" />
                        <div class="form-group">
                            <textarea name="sqlquery" placeholder="请输入SQL语句，支持批量查询，多条SQL以分号(;)分格" cols="60" rows="5" class="form-control" id="sqlquery"></textarea>
                        </div>

                        <input type="submit" class="btn btn-primary btn-embossed" value="执行" /> 
                        <input type="reset" class="btn btn-default btn-embossed" value="重置" /> 
                    </form>

                </div>
            </div>
        </div>
    </div>
    <?php include(__DIR__ . '/../common/footer.phtml'); ?>
    <script type="text/javascript">
        $(function () {
            $(".well").height($(".page-wrapper").height() - $(".page-content").height() - 90 + $(".well").height());
            $('.dropdown-menu input, .dropdown-menu label, .dropdown-menu select').on('click', function (e) {
                e.stopPropagation();
            });
            //$("select").select2({dropdownCssClass: 'dropdown-inverse'});
            //提交时检查是否有删除或清空操作
            $("#sqlexecute").submit(function () {
                var v = $("#sqlquery").val().toLowerCase();
                if ((v.indexOf("delete ") >= 0 || v.indexOf("truncate ") >= 0) && !confirm("确认删除或清空？")) {
                    return false;
                }
            });
            $("ul#subaction li input").on("click", function () {
                $("#topaction").val($(this).attr("rel"));
                return true;
            });
        });
    </script>
</body>
</html>